package com.google.coffeeshop.gui.desktop.orderpayment;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import com.google.coffeeshop.GeneralConstants;
import com.google.coffeeshop.business.MaintenanceBusiness;
import com.google.coffeeshop.entity.SeatDTO;
import com.google.coffeeshop.gui.desktop.SwingFactory;

public class SeatChoosingForStopFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	
	private SeatChoosingForStopPanel panel;
	private OrderPaymentPanel pnlOrderPayment;
	
	public SeatChoosingForStopFrame(OrderPaymentPanel pnlOrderPayment) {
		this.pnlOrderPayment = pnlOrderPayment;
		setTitle("Seat Choice");
		setSize(342, 122);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		JLabel lblNewLabel = new JLabel("Choose a seat to stop working...");
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 0;
		getContentPane().add(lblNewLabel, gbc_lblNewLabel);
		
		panel = new SeatChoosingForStopPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 1;
		getContentPane().add(panel, gbc_panel);
		
		JButton btnOK = new JButton("OK");
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				maintainSeat();
				refreshParentGUI();
				disposeFrame();
			}

			
		});
		GridBagConstraints gbc_btnOK = new GridBagConstraints();
		gbc_btnOK.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnOK.gridx = 0;
		gbc_btnOK.gridy = 2;
		getContentPane().add(btnOK, gbc_btnOK);
		
	}
	
	public void refreshParentGUI() {
		pnlOrderPayment.refreshGUI();
	}
	
	private void maintainSeat() {
		Long seatId = panel.getChosenSeatId();
		if (seatId == null) {
			return;
		}
		
		SeatDTO seat = MaintenanceBusiness.findSeatById(seatId);
		if (seat != null) {
			if (SwingFactory.showConfirmDialog("Are you sure?")) {
				MaintenanceBusiness.maintainSeat(seat.getId());
			}
		}
	}
	
	private void disposeFrame() {
		this.dispose();
	}
	
}

